<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="客户编号" prop="customerCode">
        <el-input
          v-model="queryParams.customerCode"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年级" prop="grade">
        <el-select
          v-model="queryParams.grade"
          placeholder="请选择年级"
          clearable
        >
          <el-option
            v-for="item in gradeOpiton"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="签约状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择签约状态"
          clearable
        >
          <el-option label="未签约" :value="0" />
          <el-option label="已签约" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择性别"
          clearable
        >
          <el-option label="男" :value="0" />
          <el-option label="女" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="线索客户来源" prop="customerSource">
        <el-select
          v-model="queryParams.customerSource"
          placeholder="请选择线索客户来源"
          clearable
        >
          <el-option
            v-for="item in customerSourceOpiton"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="教育顾问" prop="educationlAdviser">
        <userSelect
          v-model="queryParams.educationlAdviser"
          class="form-select"
        />
      </el-form-item>
      <el-form-item label="所属校区" prop="campus">
        <campusSelect v-model="queryParams.campus" class="form-select" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" @click="handleCreate">
          新增
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" @click="handleExport">
          导出模板
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-upload
          :action="baseUrl + '/system/customer/uploadFile'"
          :on-success="handleSuccess"
          :limit="1"
          :file-list="fileList"
          :show-file-list="false"
          :headers="headers"
          ref="upload"
        >
          <el-button size="mini" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column
        label="客户编号"
        align="center"
        prop="customerCode"
        min-width="120"
      />
      <el-table-column label="姓名" align="center" prop="customerName" />
      <el-table-column label="年级" align="center" prop="grade">
        <template #default="{ row }">
          <span>{{ formatGrade(row.grade) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属校区" align="center" prop="campusName" />
      <el-table-column label="性别" align="center">
        <template #default="{ row }">
          <div v-if="row.sex === 1">女</div>
          <div v-else>男</div>
        </template>
      </el-table-column>
      <el-table-column label="就读学校" align="center" prop="school" />
      <el-table-column label="手机" align="center" prop="phone" />
      <el-table-column
        label="线索客户来源"
        align="center"
        prop="sourceOfLeadCustomersName"
      />
      <el-table-column label="签约状态" align="center" prop="status">
        <template #default="{ row }">
          <el-tag type="success" v-if="row.status === 1">已签约</el-tag>
          <el-tag type="info" v-else>未签约</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="教育顾问"
        align="center"
        prop="educationalAdviserName"
      />
      <el-table-column label="首次接触时间" align="center" prop="firstTime" />
      <el-table-column label="上传者" align="center" prop="upBy" />
      <el-table-column label="操作" align="center" width="180px" fixed="right">
        <template #default="scope">
          <el-button
            type="text"
            size="small"
            @click="handleCreateTracking(scope.row)"
            >新增追踪记录</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handleCommand('handleEdit', scope.row)"
          >
            修改
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="handleCommand('handleView', scope.row)"
          >
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      :title="dialog.title"
      :visible.sync="dialog.visible"
      width="50%"
      center
      @close="handleClose"
    >
      <el-form :model="dialog.trackingForm" label-width="140px">
        <el-form-item label="追踪方式">
          <el-input value="电话" readonly />
        </el-form-item>
        <el-form-item label="追踪记录">
          <el-input
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 4 }"
            placeholder="请输入追踪记录"
            v-model="dialog.trackingForm.trackingRecords"
          />
        </el-form-item>
        <el-form-item label="追踪结果">
          <el-select
            v-model="dialog.trackingForm.trackingResults"
            placeholder="请选择追踪结果"
            clearable
          >
            <el-option label="继续电话" :value="0" />
            <el-option label="预约成功" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="签约可能">
          <el-select
            v-model="dialog.trackingForm.signingResults"
            placeholder="请选择签约可能"
            clearable
          >
            <el-option label="小" :value="0" />
            <el-option label="中" :value="1" />
            <el-option label="大" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="电话有效">
          <el-select
            v-model="dialog.trackingForm.isValid"
            placeholder="请选择电话有效"
            clearable
          >
            <el-option label="是" :value="1" />
            <el-option label="否" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="下次追踪日期">
          <el-date-picker
            type="date"
            placeholder="请选择下次追踪日期"
            v-model="dialog.trackingForm.nextTrackTime"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleCreatRecord">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getList, addTracking } from "@/api/leadManagement/leadClientList";
import { getToken } from "@/utils/auth";
import { exportExcel } from "@/utils/export";
import userSelect from "@/components/Select/userSelect.vue";
import campusSelect from "@/components/Select/campusSelect.vue";

export default {
  components: {
    userSelect,
    campusSelect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 定时任务表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        customerCode: "", //客户编号
        grade: "", //年级
        customerName: "", //姓名
        status: "", //签约状态
        phone: "", //手机号码
        sex: "", //性别
        customerSource: "", //线索客户来源
        educationlAdviser: "", //教育顾问
        campus: "", //所属校区
        pageNum: 1,
        pageSize: 10,
      },
      // 年级选项
      gradeOpiton: [],
      // 线索客户来源选项
      customerSourceOpiton: [],
      baseUrl: process.env.VUE_APP_BASE_API,
      fileList: [],
      headers: { Authorization: `Bearer ${getToken()}` },
      dialog: {
        visible: false, // 控制对话框的显示与隐藏
        trackingForm: {}, // 追踪记录表单数据
        reservationForm: {},
        title: "", // 对话框标题
        type: "", // 对话框类型，"tracking" 或 "reservation"
      },
    };
  },
  created() {
    this.gradeOpiton = [
      { value: 0, label: "小一" },
      { value: 1, label: "小二" },
      { value: 2, label: "小三" },
      { value: 3, label: "小四" },
      { value: 4, label: "小五" },
      { value: 5, label: "小六" },
      { value: 6, label: "初一" },
      { value: 7, label: "初二" },
      { value: 8, label: "初三" },
      { value: 9, label: "高一" },
      { value: 10, label: "高二" },
      { value: 11, label: "高三" },
      { value: 12, label: "高四" },
    ];

    this.customerSourceOpiton = [
      { value: 0, label: "推荐" },
      { value: 1, label: "户外" },
      { value: 2, label: "拉访" },
      { value: 3, label: "平面媒体" },
      { value: 4, label: "渠道" },
      { value: 5, label: "数据库数据" },
      { value: 6, label: "推广" },
      { value: 7, label: "网络" },
      { value: 8, label: "手机短信" },
      { value: 9, label: "直访" },
      { value: 10, label: "呼入" },
      { value: 11, label: "呼出" },
      { value: 12, label: "回访" },
    ];
    this.getList();
  },
  methods: {
    /** 查询定时任务列表 */
    getList() {
      this.loading = true;
      getList(this.queryParams).then((res) => {
        this.dataList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        jobId: undefined,
        jobName: undefined,
        jobGroup: undefined,
        invokeTarget: undefined,
        cronExpression: undefined,
        misfirePolicy: 1,
        concurrent: 1,
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.jobId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 更多操作触发
    handleCommand(command, row) {
      const { customerId } = row;
      switch (command) {
        case "handleEdit":
          this.$router.push({
            path: "/leadManagement/edit",
            query: { id: customerId },
          });
          break;
        case "handleView":
          this.$router.push({
            path: "/leadManagement/detail",
            query: { id: customerId },
          });
          break;

        default:
          break;
      }
    },

    handleSuccess(response, file) {
      this.$refs.upload.clearFiles();
      this.$message({
        type: "success",
        message: "上传成功",
      });
      this.getList();
    },

    formatGrade(grade) {
      return _.find(this.gradeOpiton, { value: grade })?.label || "未知年级";
    },

    handleCreate() {
      this.$router.push({
        path: "/leadManagement/create",
      });
    },

    handleCreateTracking(row) {
      this.dialog.visible = true;
      this.dialog.title = "新增追踪记录";
      this.dialog.trackingForm.customerId = row.customerId;
    },

    handleCreatRecord() {
      addTracking({
        ...this.dialog.trackingForm,
        trackMethod: 0,
      }).then((res) => {
        if (res.code === 200) {
          this.$message.success("追踪记录添加成功");
          this.dialog.visible = false;
          this.getData(this.data.customerId); // 刷新客户数据
        } else {
          this.$message.error(res.msg || "添加追踪记录失败");
        }
      });
    },

    handleClose() {
      this.dialog.visible = false;
      this.dialog.type = "";
      this.dialog.title = "";
      this.dialog.trackingForm = {};
      this.dialog.reservationForm = {};
    },

    handleExport() {
      exportExcel("分配线索客户模板", "/system/excel/export");
    },
  },
};
</script>
